package com.itheima.evaluate.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.model.evaluate.pojos.Appraisal;
import com.car.model.mongo.acccomment.dtos.AccCommentDto;
import com.car.model.mongo.acccomment.pojos.AccComment;
import com.car.utils.IdWorker;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.itheima.evaluate.mapper.AccCommentMapper;
import com.itheima.evaluate.service.AccCommentService;
import com.itheima.evaluate.service.AccessoriesEvaluationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class AccCommentServiceImpl extends ServiceImpl<AccCommentMapper, AccComment> implements AccCommentService {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    AccessoriesEvaluationService accessoriesEvaluationService;

    @Override
    public ResponseResult saveComment(AccCommentDto dto) {

        //检查参数
        if (dto == null || dto.getCustomerId() == null || dto.getOrderId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AccComment accComment = new AccComment();

        //拷贝属性
        BeanUtils.copyProperties(dto,accComment);
        //生成唯一的id
        IdWorker idWorker=new IdWorker(0,0);
        accComment.setAccCommentId(idWorker.nextId());
        //将评论信息添加到mongoDB中
        mongoTemplate.save(accComment);

        //将评价信息保存到数据库
        Appraisal appraisal = new Appraisal();
        appraisal.setCreateTime(new Date());
        appraisal.setCustomerId(dto.getCustomerId());
        appraisal.setCustomerName(dto.getCustomerName());
        appraisal.setIsDelete((short) 0);
        appraisal.setLogisticsScore(dto.getEvaluationScore());
        appraisal.setOrderCreateTime(dto.getOrderTime());
        appraisal.setOrderId(Integer.parseInt(dto.getOrderId()));
        appraisal.setQualityScore(dto.getEvaluationScore());
        appraisal.setStoreScore(dto.getEvaluationScore());
        appraisal.setServiceClassName(dto.getServiceClassName());
        appraisal.setStoreSimpleName(dto.getStoreName());
        appraisal.setServiceItems(dto.getServiceClassName());
        appraisal.setStoreId(1);
        appraisal.setStoretype("1");
        accessoriesEvaluationService.save(appraisal);
        //返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
